/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sa_prakt1.bl.services.impl.standard;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import sa_prakt1.bl.entity.impl.standard.DefaultEntry;
import sa_prakt1.bl.entity.interfaces.Entry;
import sa_prakt1.bl.entity.interfaces.Forum;
import sa_prakt1.bl.entity.interfaces.Member;
import sa_prakt1.bl.services.interfaces.Community;
import sa_prakt1.bl.services.interfaces.SamCommCreator;

/**
 *
 * @author Andreas
 */
public class DefaultCommunity implements Community {

    private static DefaultCommunity instance;
    protected List<Forum> forenListe = new ArrayList<>();
    protected List<Member> memberListe = new ArrayList<>();
    private static final String filepath = "./serialize.ser";

    protected DefaultCommunity() {
      deSerialize();
    }

    /**
     * Singleton pattern
     *
     * @return Community
     */
    public static DefaultCommunity getInstance() {
        if (instance == null) {
            instance = new DefaultCommunity();
        }

        return instance;
    }

    @Override
    public void addMember(Member m) {
        if (checkNickname(m.getNickname())) {
          this.memberListe.add(m);
          serialize();
        } else {
          System.err.println("Member schon vorhanden!");
        }
    }

    @Override
    public void registerMemberInForum(Member m, Forum f) {
        //Memberliste und forenliste jeweils updaten
        m.getSubscriptions().add(f);
        f.getMembers().add(m);
        serialize();
    }

    @Override
    public void addForum(Member m, Forum f) {
        //Ersteller ist auch gleichzeitig Member
        if (checkForum(f)) {
          f.getMembers().add(m);
          this.forenListe.add(f);
          serialize();
        } else {
          System.err.println("Forum schon vorhanden");
        }
    }

    @Override
    public void addEntry(Member author, Forum forum, String title, String text) {

        String id = "de1";
        Entry e = new DefaultEntry(id, title, text, author, forum);

        //Entsprechendes Forum suchen und Eintrag hinzufuegen
        for (Forum f : this.forenListe) {
            if (f.getOID().equals(forum.getOID())) {
                f.getEntries().add(e);
            }
        }
        serialize();
    }

    @Override
    public boolean login(String nick, String pw) {
        for (Member m : this.memberListe) {
            if ((m.getNickname().equals(nick))
                    && (m.getPassword().equals(pw))) {
                return true;
            }
        }

        return false;
    }

    @Override
    public List<Entry> findEntriesByAuthor(Member m) {
        ArrayList<Entry> tmp = new ArrayList<>();

        for (Forum f : this.forenListe) {
            for (Entry e : f.getEntries()) {
                if (e.getWrittenBy().getOID().equals(m.getOID())) {
                    tmp.add(e);
                }
            }
        }

        return tmp;
    }

    @Override
    public List<Entry> findEntriesByTitle(String title) {
        ArrayList<Entry> tmp = new ArrayList<>();

        for (Forum f : this.forenListe) {
            for (Entry e : f.getEntries()) {
                if (e.getTitle().equals(title)) {
                    tmp.add(e);
                }
            }
        }

        return tmp;
    }

    @Override
    public SamCommCreator getCreator() {
        return DefaultSamCommCreator.getInstance();
    }

    protected void serialize() {
      try {
        FileOutputStream f = new FileOutputStream(filepath);
        ObjectOutputStream out = new ObjectOutputStream(f);
        out.writeObject(this);
        out.flush();
        out.close();
      } catch (IOException e) {
        System.err.println("Error: Serialize()");
      }
    }

    protected void deSerialize() {
      int i = 0;
      try {
        FileInputStream f = new FileInputStream(filepath);
        ObjectInputStream in = new ObjectInputStream(f);
        try {
          Community dc = (DefaultCommunity)in.readObject();
          this.forenListe = dc.getForenlist();
          this.memberListe = dc.getMemberlist();
        } catch (Exception e) {
          System.err.println("Error - deSerialize: Community-Object");
        }
      } catch (Exception e) {
        System.err.println("Error - deSerialize: Get Streams");
      }
    }

    @Override
    public List<Forum> getForenlist() {
      return this.forenListe;
    }

    @Override
    public List<Member> getMemberlist() {
      return this.memberListe;
    }

    private boolean checkNickname(String nickname) {
      boolean result = true;
      for (Member m : memberListe) {
        result = !(m.getNickname().equals(nickname));
      }
      return result;
    }

  private boolean checkForum(Forum f) {
    boolean result = true;
    for (Forum tmp : forenListe) {
      result = !(tmp.getTitle().equals(f.getTitle()));
    }
    return result;
  }
}
